﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using Rodunu.Movies.Contracts;

namespace Rodunu.Movies.Logic.User
{
    internal static class UserHelper
    {

        internal static string CreateSalt(int size)
        {
            // Generate a cryptographic random number using the cryptographic
            // service provider
            var rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[size];
            rng.GetBytes(buff);
            // Return a Base64 string representation of the random number
            return Convert.ToBase64String(buff);
        }

        internal static string CreatePasswordHash(string pwd, string salt)
        {
            string saltAndPwd = String.Concat(pwd, salt);
            string hashedPwd =
                  FormsAuthentication.HashPasswordForStoringInConfigFile(
                                                       saltAndPwd, "SHA1");
            hashedPwd = String.Concat(hashedPwd, salt);
            return hashedPwd;
        }

        internal static Contracts.UserInfo CreateUser(Contracts.UserInfo user)
        {
            UserInfo userInfo = new Contracts.UserInfo(user.Id, user.UserName, user.FirstName, user.LastName, 
                user.Password, user.ShowProfile, user.DateAdded, user.LastModifiedBy, user.EmailAddress, user.Status);
            userInfo.AllowToViewQueue = user.AllowToViewQueue;
            userInfo.Avatar = user.Avatar;
            userInfo.LoggedInDate = user.LoggedInDate;
            userInfo.NetflixId = user.NetflixId;

            return userInfo;
        }
    }
}
